#some more functions called from boot scripts in Puppy4...
#v411 moved choosemousefunc() out of /usr/sbin/xorgwizard, want call from rc.sysinit.
#w468 not using set_mixers() anymore, it's done in /etc/init.d/alsa.
#w468 choosemousefunc() simplified.
#101120 new firmware_tarball_func, as /sbin/pup_event_backend_modprobe rewritten.
#110722 usb drive, LED light briefly flashes after unmounted, so wait a bit. 110828 remove.
#120209 slight changes so that SSS mechanism can substitute translations. (see /usr/share/sss/script_strings)
#120828 rerwin: record firmware installed in /etc/modules/firmware.dep.inst.${KERNVER}
#120828 grep needs -E option, as expression is extended regular expression.
#120831 rerwin: Obtain firmware from all-firmware directories as well as tarballs.
#121004 rerwin: /etc/modules/firmware.dep.inst.${KERNVER} now has names of firmware tarballs, not modules.

#101119 called from rc.sysinit...
#global $MODULE is name of module, global $KERNVER.
firmware_tarball_func() {
 MODULEx="`echo -n "$MODULE" | tr '_' '-'`"
#121004 ... 
 fPATTERN='[:,]'"${MODULE}"'\.ko|[:,]'"${MODULEx}"'\.ko'
 FIRMPKG="`cat /etc/modules/firmware.dep.${KERNVER}  | grep -v '^#' | grep ':' | grep -E "$fPATTERN" | cut -f 1 -d ':' | head -n 1`"
 if [ "$FIRMPKG" != "" ];then
  iPATTERN='^'"${FIRMPKG}"'$'
  if [ "`grep "$iPATTERN" /etc/modules/firmware.dep.inst.${KERNVER}`" = "" ];then
   #120831 rerwin: ...
   FLAGFIRM='no'
   if [ -d /lib/modules/all-firmware/${FIRMPKG} ];then #support firmware directories.
    cp -a -f --remove-destination /lib/modules/all-firmware/${FIRMPKG}/* /
    FLAGFIRM='yes'
   else
    if [ -f /lib/modules/all-firmware/${FIRMPKG}.tar.gz ];then
     tar -z -x --strip=1 --directory=/ -f /lib/modules/all-firmware/${FIRMPKG}.tar.gz > /dev/null 2>&1
     FLAGFIRM='yes'
    fi
   fi
   if [ "$FLAGFIRM" = "yes" ];then #120831 end
    #execute any post-install script...
    if [ -f /pinstall.${FIRMPKG}.sh ];then
     BRKCNT=0; SLEEPU=$((${$} * 10))
     while [ 1 ];do #serialise execution of pinstall scripts...
      PINSTALLCNT=`find / -maxdepth 1 -type f -name 'pinstall.*.sh' | wc -l`
      [ $PINSTALLCNT -eq 1 ] && break
      usleep $SLEEPU
      BRKCNT=$(($BRKCNT + 1))
      [ $BRKCNT -gt 5 ] && break
     done
     #120828 remove...
     #echo "$MODULE" >> /etc/modules/firmware.dep.inst.${KERNVER}
     cd /
     /pinstall.${FIRMPKG}.sh >/dev/null 2>&1
     rm -f /pinstall.${FIRMPKG}.sh >/dev/null 2>&1
    fi
    echo "$FIRMPKG" >> /etc/modules/firmware.dep.inst.${KERNVER} #120828
   fi
  fi
 fi
}


#v403 change desktop icon to unmounted status, called from pup_event_frontend_d, pmount...
icon_unmounted_func() {
 #passed params: drivename category  ex: sda usbdrv
 DRVICON="drive48.png" #default.
 [ "$2" = "usbdrv" ] && DRVICON="usbdrv48.png"
 [ "$2" = "card" ] && DRVICON="card48.png"
 [ "$2" = "floppy" ] && DRVICON="floppy48.png"
 [ "$2" = "optical" ] && DRVICON="optical48.png"
 #find out if already done...
 if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
  GLOBLINES="`cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons | tr '\n' '|' | sed -e 's%</rule>|%\n%g'`"
  diPATTERN='/root/.pup_event/drive_'"${1}"'"'
  [ "`echo "$GLOBLINES" | grep "$diPATTERN" | grep "$DRVICON"`" != "" ] && return
 fi
 #[ "`echo "${ATADRIVES}" | grep "${1}"`" = "" ] && sleep 1 #110722 usb drive, LED light briefly flashes after unmounted, so wait a bit. ATADRIVES is in /etc/rc.d/PUPSTATE.
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
   <Icon>/usr/local/lib/X11/pixmaps/${DRVICON}</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
}

icon_mounted_func() {
 #passed params: drivename category  ex: sda usbdrv
 DRVICON="drive_mntd48.png" #default.
 [ "$2" = "usbdrv" ] && DRVICON="usbdrv_mntd48.png"
 [ "$2" = "card" ] && DRVICON="card_mntd48.png"
 [ "$2" = "floppy" ] && DRVICON="floppy_mntd48.png"
 [ "$2" = "optical" ] && DRVICON="optical_mntd48.png"
 imPATTERN='^/dev/'"${1}" #important, no space on end.
# if [ "`mount | cut -f 1,3 -d ' ' | grep "$imPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
 if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$imPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
  #only partitions mntd on /initrd/* then must be mntd at boot. cannot unmount.
  #also, full hd install has partition mntd on '/'.
  DRVICON="drive_mntd_boot48.png" #default.
  [ "$2" = "usbdrv" ] && DRVICON="usbdrv_mntd_boot48.png"
  [ "$2" = "card" ] && DRVICON="card_mntd_boot48.png"
  [ "$2" = "floppy" ] && DRVICON="floppy_mntd_boot48.png"
  [ "$2" = "optical" ] && DRVICON="optical_mntd_boot48.png"
 fi
 #find out if already done...
 if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
  GLOBLINES="`cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons | tr '\n' '|' | sed -e 's%</rule>|%\n%g'`"
  diPATTERN='/root/.pup_event/drive_'"${1}"'"'
  [ "`echo "$GLOBLINES" | grep "$diPATTERN" | grep "$DRVICON"`" != "" ] && return
 fi
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
   <Icon>/usr/local/lib/X11/pixmaps/${DRVICON}</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
}


icon_remove_func() {
 #passed param is drive icon to remove from desktop. ex: sda
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <UnsetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
  </UnsetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
}

choosemousefunc() { #w468 called from xorgwizard.
 MOUSEINTRO="Only very old PCs may have a serial mouse. These are recognised by the fairly large rectangular plug, technically known as a DB9 or the even larger DB25 (where the 9 or 25 refers to the number of pins in the plug). Press the UP and DOWN arrow keys to choose your mouse type, then press the ENTER key:"
 DETECTEDMOUSEDEV="`cat /etc/mousedevice`"
 case $DETECTEDMOUSEDEV in
  ttyS*)
   MOUSEMENU1a='serial'; MOUSEMENU1b='(mouse in very old computers)' #ttyS leave this comment, needed by SSS
   MOUSEMENU2a='usb/ps-2'; MOUSEMENU2b='(mouse or touchpad in all modern computers)' #ttyS comment needed by SSS
  ;;
  *)
   MOUSEMENU1a='usb/ps-2'; MOUSEMENU1b='(mouse or touchpad in all modern computers)' #usb-ps2 comment needed by SSS
   MOUSEMENU2a='serial'; MOUSEMENU2b='(mouse in very old computers)' #usb-ps2 comment needed by SSS
  ;;
 esac
 dialog --no-cancel --menu "${MOUSEINTRO}\n\n
Please choose mouse type..." 0 0 0 "$MOUSEMENU1a" "$MOUSEMENU1b" "$MOUSEMENU2a" "$MOUSEMENU2b" 2> /tmp/testmsx
 [ $? -ne 0 ] && return 1
 CHOSENMOUSETYPE="`cat /tmp/testmsx`"
 case $CHOSENMOUSETYPE in
 serial)
  dialog --no-cancel --menu "Serial port mouse plugged into...\nUse UP/DOWN ARROW keys, ENTER key to finish" 0 0 0 ttyS0 "COM1 (most likely)" ttyS1 COM2 ttyS2 COM3 ttyS3 COM4 2> /tmp/testmsx
  [ $? -ne 0 ] && return 1
  MOUSEDEV="`cat /tmp/testmsx`"
  modprobe sermouse
  ln -snf $MOUSEDEV /dev/mouse
  echo -n "$MOUSEDEV" > /etc/mousedevice
  cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\w\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "Microsoft" #mouse0protocol/' > /tmp/xorg.conf.tmp2
 ;;
 *)
  echo -n "input/mice" > /etc/mousedevice
  ln -snf input/mice /dev/mouse
  cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\w\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "IMPS\/2" #mouse0protocol/' > /tmp/xorg.conf.tmp2
 ;;
 esac
 sync
 cp -f /tmp/xorg.conf.tmp2 /etc/X11/xorg.conf
 rm -f /tmp/xorg.conf.tmp2
} #choosemousefunc()


###END###
